package com.bus.controller;

import com.bus.entity.Task;
import com.bus.service.ProviderService;
import com.bus.service.TaskService;
import com.bus.vo.TaskVo;
import com.common.DataGridView;
import com.common.ResultObj;
import com.study.entity.Details;
import com.study.entity.PurChaseVo;
import com.study.service.DetailsService;
import com.study.service.PurChaseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/bus/inport")
public class purChaseController {
    @Resource
    PurChaseService purChaseService;

    @Resource
    DetailsService detailsService;

    @Resource
    ProviderService providerService;

    @Resource
    TaskService taskService;

    /**
     * 分页查询、模糊查询商品信息
     * @param purChaseVo
     * @return
     */
    @RequestMapping("/loadInport")
    @ResponseBody
    public DataGridView loadInport(PurChaseVo purChaseVo){
        return purChaseService.selectAllInport(purChaseVo);
    }

    @RequestMapping(value = "/inportManagement",method = RequestMethod.GET)
    public String showInports() {
        return "forward:/view/resp/inportManager.jsp";
    }

    @RequestMapping(value = "/addInport")
    @ResponseBody
    public ResultObj addInport(PurChaseVo purChaseVo){
        try {
            purChaseVo.setPurchaseOrderNo("CGDD"+System.currentTimeMillis());
            purChaseVo.setOrderPrice(purChaseVo.getGoodsNum()*purChaseVo.getPur_price());
            Details details=new Details();
            purChaseService.insert(purChaseVo);
            details.setPurchaseOrderNo(purChaseVo.getPurchaseOrderNo());
            details.setGoodsId(purChaseVo.getGoodsId());
            details.setGoodsNum(purChaseVo.getGoodsNum());
            detailsService.insert(details);
            TaskVo task=new TaskVo();
            taskService.Delete(purChaseVo.getId());
            task.setId(purChaseVo.getId());
            task.setTaskName(purChaseVo.getGoodsName()+"商品入库");
            task.setParentId(2);
            task.setHref("bus/inport/inportManagementIn");
            task.setTaskType("商品入库");
            taskService.Insert(task);
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    @RequestMapping(value = "/inportManagementIn",method = RequestMethod.GET)
    public String showInportsIn() {
        return "forward:/view/data/inportManagerIn.jsp";
    }

    @RequestMapping("/OnceAgain")
    @ResponseBody
    public ResultObj OnceAgain(PurChaseVo purChaseVo){
        try {
            TaskVo task=new TaskVo();
            taskService.Delete(purChaseVo.getId());
            task.setId(purChaseVo.getId());
            task.setTaskName(purChaseVo.getGoodsName()+"商品入库");
            task.setParentId(2);
            task.setHref("bus/inport/inportManagementIn");
            task.setTaskType("商品入库");
            taskService.Insert(task);
            purChaseService.OnceAgain(purChaseVo.getId());
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.AGAIN_ERROR;
        }
        return ResultObj.AGAIN_SUCCESS;
    }
}
